﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
namespace GameUnit
{
    public class EffectDamage : Effect
    {
        public enum DamageType
        {
            AOE = 2,
            ShortRange = 1,
        }
        public int Power;
        public DamageType damageType;
        public Area SearchArea; 
        public override void Init(Hashtable config,Unit srcUnit)
        {
            base.Init(config,srcUnit);
            this.Power = (int)config[":power"];
            this.damageType = (DamageType)(int)config[":damage-type"];
            if (config.ContainsKey(":area"))
            {
                this.SearchArea = new Area();
                this.SearchArea.Init((Hashtable)config[":area"]);
            }
        }

        public override void Exec()
        {
            switch (this.damageType)
            {
                case DamageType.AOE:
                  foreach (KeyValuePair<long,Unit> unit in UnitManager.Units)
                  {
                        if (unit.Value.UnitId==this.SrcUnit.UnitId)
                        {
                            continue;
                        }
                       float posX = unit.Value.transform.position.x - this.SrcUnit.transform.position.x;
                       float posY = unit.Value.transform.position.y - this.SrcUnit.transform.position.y;
                       if (this.SearchArea.IsInSide(posX, posY,this.SrcUnit.faceDir))
                       {
                            unit.Value.HP -= this.Power;
                       }
                  }
                break;
                case DamageType.ShortRange:
                    this.DstUnit.HP -= this.Power;
                break;
            }
        }
    }
}